import global from './config.js' //全局配置
import request from './request.js' //网络接口请求
import tool from './tool.js' //工具方法
import mixin from './mixin.js' //混入
import * as Pinia from 'pinia'; //引入pinia
import stores from "./stores" //
import update from './update.js' //app 升级
update()
const install = app => {

	app.use(Pinia.createPinia());
	app.mixin(mixin) //全局混入
	let store = stores()
	uni.$store = store
	//以下是全局方法
	let globalProperties = {
		update: update,
		global: global, //全局配置  接口地址等
		baseUrl: global.baseUrl, //接口地址
		store: store, //全局配置  接口地址等
		request: request, //请求逻辑
		zs: '#00BEC3', //主色系
		fs: '#FF9C37', //主色系
		background: {
			backgroundImage: 'linear-gradient(180deg, #00BEC3, #16c3c8)'
		}, //主色系linear-gradient(180deg, #00BEC3 0%, #16c3c8 100%)
		// background:{backgroundImage:'linear-gradient(45deg, rgb(0, 190, 195), rgb(22, 195, 200))'}, //主色系linear-gradient(180deg, #00BEC3 0%, #16c3c8 100%
		tool: tool, //工具栏
		msg: tool.msg, //提示
		jump: tool.jump, //最常用局内跳转
		jumps: tool.advJump, //超级跳转
		advJump: tool.advJump, //超级跳转
		navBack: tool.navBack, //返回上一页
		refresh: tool.refresh, //刷新页面 当前页面onLoad
		showModal: tool.showModal, //二次确认框
		log: (e) => console.log(e), //常用于页面视图层log 点击回调参数等
		getData: (key) => store.getData(key), //读取缓存
		setData: (key, data) => store.setData(key, data), //设置缓存

		goDemo(item) {
			tool.jump('/pages/index/demo/downOrder', {
				service_id: item.service_id || item.id,
				hospital_id: item.hospital_id || '',
				master_id: item.master_id || '',
			})
		}, //设置缓存 

		location() {
			return new Promise((resolve, reject) => {
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						let aa = res.latitude + ',' + res.longitude
						globalProperties.setData('location', aa)
						resolve(res)
					},
					fail(err) {
						reject(err)
					},
				});
			})
		},

		//富文本页面
		goText(params) {
			if (params.detail) store.setData('richText', params.detail), params.detail = 'richText';
			globalProperties.jump('/pages/public/text', params)
		},

		getUser(params) { //用户信息接口
			return new Promise((resolve) => {
				request('/member/my/info', params).then(res => {
					let data = res.data
					store.setData('userInfo', data) //存到vuex
					resolve(data)
				});
			})
		},
		getConfig() { //全局基本配置信息接口
			return new Promise((resolve) => {
				let config = uni.getSystemInfoSync()
				request('/master/commom/config', config).then(res => {
					let data = res.data
					store.setData('config', data) //存到vuex
					resolve(data)
				})
			})
		},
		login_success(res, event = 'back') { //登录成功
			tool.msg(res.msg)
			if (res.data.type) { //绑定手机号
				tool.jump('/pages/login/bindMobile', res.data, event == 'back' ? 'redirectTo' : 'to')
				return
			}
			uni.removeStorageSync('regcode') //清除邀请码
			store.setData('token', res.data?.token?.access_token || '') //存到vuex
			globalProperties.getUser()

			setTimeout(() => { //跳转
				if (event == "back") tool.navBack() //返回
				this.$nextTick(() => {
					setTimeout(tool.refresh, 20)
				})
			}, 800)
			store.chat_start()
		},
		list: (...fidle) => { //每个项目单独数据
			let obj = {
				banner: [{
						image: 'https://cdn.uviewui.com/uview/swiper/1.jpg',
						title: '昨夜',
						price: 26,
						id: 1,
						url: 'https:www.baidu.com'
					},
					{
						image: 'https://cdn.uviewui.com/uview/swiper/2.jpg',
						title: '身无彩凤双飞翼，心有灵犀一点通',
						price: 999.99,
						id: 2
					},
					{
						image: 'https://cdn.uviewui.com/uview/swiper/3.jpg',
						title: '谁念西风独自凉，萧萧黄叶闭疏窗，沉思往事立残阳',
						price: 59.1,
						id: 3
					}
				],

				'state_auth': {
					'-1': '未实名',
					'0': '申请中',
					'1': '已通过',
					'2': '已驳回',
				},
				'name_auth': {
					'-1': '未实名',
					'0': '申请中',
					'1': '已通过',
					'2': '已驳回',
				},
				sex: {
					1: '男',
					2: '女',
				},
			}
			let value = obj
			fidle.map(e => value = value?.[e])
			return value
		},
	}

	uni.$z = globalProperties
	app.config.globalProperties.$z = globalProperties
	for (let key in globalProperties) {
		app.config.globalProperties['$' + key] = globalProperties[key]
	}
}

export default {
	install
}